package o;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkRequest.Builder;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Looper;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.card.base.bean.AppInfoBean;
import java.util.List;
import o.dkb.d;

public class dtz {
    private static dtz ˊ = new dtz();
    private static final Object ˋ = new Object();
    private static boolean ˏ = true;
    private static final String ॱ = dtz.class.getSimpleName();
    private i ʻ;
    private f ʼ;
    private e ˎ = new e(null);

    private dtz() {
        ʼ();
    }

    public static dtz ॱ() {
        dtz o_dtz;
        synchronized (ˋ) {
            if (ˊ == null) {
                ˊ = new dtz();
            }
            o_dtz = ˊ;
        }
        return o_dtz;
    }

    public static boolean ˎ() {
        Logger.ˎ("APP_SYN", ॱ + " isFirstSynApp=" + ˏ);
        return ˏ;
    }

    public static void ॱ(boolean z) {
        ˏ = z;
    }

    public void ˊ() {
        if (dhq.ॱ().ˎ()) {
            Logger.ˎ("APP_SYN", ॱ + " processDownload");
            cfw.ˎ(new dtv(), new 5(this));
        }
    }

    private void ʼ() {
        Logger.ˎ("APP_SYN", ॱ + " beginDownload");
        ContentManager.getInstant().getContext().registerReceiver(this.ˎ, new IntentFilter(dvq.ˏ()));
        if (VERSION.SDK_INT >= 24) {
            this.ʼ = new f();
            Builder builder = new Builder();
            builder.addCapability(12);
            ((ConnectivityManager) ContentManager.getInstant().getContext().getSystemService(ConnectivityManager.class)).registerNetworkCallback(builder.build(), this.ʼ);
            return;
        }
        this.ʻ = new i();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        ContentManager.getInstant().getContext().registerReceiver(this.ʻ, intentFilter);
    }

    private void ʽ() {
        if (dhq.ॱ().ˎ()) {
            Logger.ˎ("APP_SYN", ॱ + " doDownload");
            List ˋ = dty.ॱ().ˋ();
            if (CollectCheck.isEmpty(ˋ)) {
                Logger.ˎ("APP_SYN", ॱ + " doDownload download queue is empty");
                return;
            }
            AppInfoBean appInfoBean = (AppInfoBean) ˋ.get(0);
            boolean ʽ = dvt.ˏ().ʽ();
            if (ʽ) {
                Logger.ˎ("APP_SYN", ॱ + " doDownload hasDownloadingTask=" + ʽ);
            } else if (!CollectCheck.isEmpty(dvt.ˏ().ˏ(appInfoBean.ˏॱ()))) {
                Logger.ˎ("APP_SYN", ॱ + " doDownload already in foreground download queue. pkg=" + appInfoBean.ˏॱ());
                ˋ(appInfoBean);
            } else if (dty.ॱ().ˎ().equals(appInfoBean.ˏॱ())) {
                Logger.ˎ("APP_SYN", ॱ + " doDownload pkg=" + appInfoBean.ˏॱ() + " is downloading.");
            } else {
                Logger.ˎ("APP_SYN", ॱ + " doDownload pkg=" + appInfoBean.ˏॱ());
                ˏ(appInfoBean);
            }
        }
    }

    private void ˏ(AppInfoBean appInfoBean) {
        Logger.ˎ("APP_SYN", ॱ + " startTask pkg=" + appInfoBean.ˏॱ());
        if (dtw.ˏ(appInfoBean)) {
            dtw.ॱ(appInfoBean, new 2(this, appInfoBean));
            return;
        }
        Logger.ˎ("APP_SYN", ॱ + " startTask fail ");
        ˋ(appInfoBean);
    }

    private void ˋ(AppInfoBean appInfoBean) {
        dty.ॱ().ˏ(appInfoBean);
        dty.ॱ().ˊ("");
        ʽ();
    }

    private void ॱ(AppInfoBean appInfoBean, d dVar) {
        if (appInfoBean == null) {
            Logger.ˋ("APP_SYN", ॱ + " turn2DownloadTask bean is null");
            if (dVar != null) {
                dVar.ˋ(null);
                return;
            }
            return;
        }
        new dqw().ˏ(dqw.ˊ(appInfoBean.ˏॱ(), appInfoBean.ˈ(), appInfoBean.ʻ()), new d(appInfoBean, dVar));
    }

    public void ˋ() {
        Logger.ˎ("APP_SYN", ॱ + " pauseDownload by wifi state change");
        if (!dtw.ˋ()) {
            ˋ(true, null);
        } else if (Logger.ˎ()) {
            Logger.ˏ("APP_SYN", ॱ + " is wifi , not do pause.");
        }
    }

    private void ˋ(boolean z, String str) {
        String ˎ = dty.ॱ().ˎ();
        if (!dof.ˋ(ˎ)) {
            SessionDownloadTask ˋ = dvt.ˏ().ˋ(ˎ);
            if (ˋ == null) {
                Logger.ˎ("APP_SYN", ॱ + " pauseImmediately fail, task not in downMgr pkg=" + ˎ);
            } else if (ˊ(z, str, ˋ.ˊˊ())) {
                dvt.ˏ().ॱ(ˋ.ʾ());
                Logger.ˎ("APP_SYN", ॱ + " pauseImmediately pauseInstantTask pkg=" + ˎ);
                dty.ॱ().ˊ("");
            }
        }
    }

    private boolean ˊ(boolean z, String str, String str2) {
        return z || str == null || !str.equals(str2);
    }

    private void ॱॱ() {
        Logger.ˎ("APP_SYN", ॱ + " showFirstTimeStartToast ");
        if (!ˎ()) {
            return;
        }
        if (epv.ᐝ(ContentManager.getInstant().getContext())) {
            new Handler(Looper.getMainLooper()).post(new b(null));
            ॱ(false);
            return;
        }
        Logger.ˎ("APP_SYN", ॱ + " showFirstTimeStartToast not on foreground ");
    }

    public void ˏ() {
        ContentManager.getInstant().getContext().unregisterReceiver(this.ˎ);
        try {
            if (VERSION.SDK_INT >= 24) {
                ((ConnectivityManager) ContentManager.getInstant().getContext().getSystemService(ConnectivityManager.class)).unregisterNetworkCallback(this.ʼ);
            } else {
                ContentManager.getInstant().getContext().unregisterReceiver(this.ʻ);
            }
            ContentManager.getInstant().getContext().unregisterReceiver(this.ˎ);
        } catch (Throwable e) {
            Logger.ˋ(ॱ, "unregisterReceiver error, e: ", e);
        }
        dty.ॱ().ˊ("");
    }
}
